<?php
class Zend_Controller_Action_Helper_User extends Zend_Controller_Action_Helper_Abstract{

	public function getIp(){
		
		if(!empty($_SERVER["HTTP_CLIENT_IP"])) $cip = $_SERVER["HTTP_CLIENT_IP"];
		
		else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
		
			$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
		
		}
		
		else if(!empty($_SERVER["REMOTE_ADDR"])) {
			
			$cip = $_SERVER["REMOTE_ADDR"];
			
		}
		
		else $cip = "无法获取！";
		
		return $cip;	
	}
	
	public function getinfo($moduleName,$controllerName){
		
		$view=$this->getActionController()->view;
		
		$member=new CMember();
		$isUser=$member->isUser($moduleName,$controllerName);
		
		if($isUser){			
			$view->assign('uid',$_COOKIE[COOKIE_PRE.'uid']);
			
			$view->assign('username',$_COOKIE[COOKIE_PRE.'username']);
			$view->assign('nickname',$_COOKIE[COOKIE_PRE.'nickname']);
			
			$view->assign('member_type',$_COOKIE[COOKIE_PRE.'member_type']);
			
			$typeArray=array('seller'=>'admins','buyer'=>'adminb','administrator'=>'administrator');
			
			$userType=$typeArray[$_COOKIE[COOKIE_PRE.'member_type']];
			
			$view->assign('userType',$userType);
			
			$order=new COrder();
			
			$orderCounts=$order->getAllOrderCounts($_COOKIE[COOKIE_PRE.'uid'],"has_shutted='0' AND ".R_PRE."orders.flid='1'");
			
			$view->assign('orderCounts',$orderCounts);
			
			$session=new Zend_Session_Namespace('shopping');
		
			if($session->shoppingSession==''){
			
				$session->shoppingSession=md5(time().$_COOKIE[COOKIE_PRE.'username']);
			
			}
			
			$this->getNewPm();
			
			$this->getNewOrders();
			
		}
		
		$this->getShowItemCounts();
		
		$view->assign('isUser',$isUser);
		
	}
	
	public function getShowItemCounts(){
		
		$view=$this->getActionController()->view;
		
		$cookieString='';
		$member = new CMember();
        if (!$member->isUser($this->getActionController()->getRequest()->getModuleName(),$this->getActionController()->getRequest()->getControllerName())||$_COOKIE[COOKIE_PRE.'member_type']!='buyer'){
        	//未登录
	        if(!empty($_COOKIE[COOKIE_PRE.'product_buy_session'])){
				$cart = new CCart();			
				$cookieString=$cart->getCartStr($_COOKIE[COOKIE_PRE.'product_buy_session']);			
			}
			
			$showItemCounts=0;
			
			if($cookieString!=''){
			
				$shoppingArray=json_decode(stripslashes($cookieString),true);
	
				$showItemCounts=count($shoppingArray);
				
			}
        } else {
        	$cart = new CCart();			
			$cookieString=$cart->getCartStrByUid($_COOKIE[COOKIE_PRE.'uid']);	
       		$showItemCounts=0;
			if($cookieString!=''){
				$shoppingArray=json_decode(stripslashes($cookieString),true);
				$showItemCounts=count($shoppingArray);
			}
        }		
		
		$view->assign('showItemCounts',$showItemCounts);
		
	}
	
	public function getNewPm(){
		
		if($_COOKIE[COOKIE_PRE.'uid']!=''){
		
			$view=$this->getActionController()->view;
			
			$pm=new CPm();
			
			$pmCounts=$pm->getNewPmCounts($_COOKIE[COOKIE_PRE.'uid']);
			
			$view->assign('pmCounts',$pmCounts);
			
			$typeArray=array('seller'=>'admins','buyer'=>'adminb','administrator'=>'administrator');
				
			$userType=$typeArray[$_COOKIE[COOKIE_PRE.'member_type']];
			
			$view->assign('userType',$userType);
		
		}
		
	}
	
	public function getNewOrders(){
		
		$view=$this->getActionController()->view;
		
		$orderCounts=0;
		
		if($_COOKIE[COOKIE_PRE.'uid']!=''){
			
			$order=new COrder();
				
			$orderCounts=$order->getAllOrderCounts($_COOKIE[COOKIE_PRE.'uid'],"has_shutted='0' AND ".R_PRE."orders.flid='1'");
		
		}
		
		$view->assign('orderCounts',$orderCounts);
		
	}
	
	public function encodePassword($userpass,$username,$time,$ip){
		
		return md5(md5($userpass).$username.$time.$ip);
		
	}
	

	
	private function updateOnline(){
		
		
		
	}
	
}
